using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

public abstract class PanelSingleton<T> : BaseMono where T : BaseMono
{

    private static T _instance;
    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                UIAttr at = typeof(T).GetCustomAttribute<UIAttr>();
                if (at != null)
                {
                    {
                        GameObject obj = Resources.Load<GameObject>(at.UIName);

                        var target = GameObject.Instantiate<GameObject>(obj, PanelRoot.Instance.transform);
                        _instance = target.GetComponent<T>();
                    }
                }
            }
            return _instance;
        }
    }

    protected virtual void Awake()
    {
        _instance = this as T;
    }
    private bool _active = false;

    public virtual bool IsActive
    {
        get
        {
            return _active;
        }
    }


    public virtual void Open()
    {
        // ChangeLayer(this.transform,"UI");
        this.transform.localScale = Vector3.one;
        // this.gameObject.layer = LayerMask.NameToLayer("UI");
        _active = true;
    }

    public virtual void Close()
    {
        this.transform.localScale = Vector3.zero;

        // ChangeLayer(this.transform,"Black");
        // this.gameObject.layer = ;
        _active = false;
    }

    void ChangeLayer(Transform trans, string targetLayer)
    {
        if (LayerMask.NameToLayer(targetLayer) == -1)
        {
            return;
        }

        trans.gameObject.layer = LayerMask.NameToLayer(targetLayer);
        foreach (Transform child in trans)
        {
            ChangeLayer(child,targetLayer);
        }
    }
}
